#!/usr/bin/env python                                                        

#
# This file is part of PopCulture
#
# PopCulture is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# PopCulture is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PopCulture.  If not, see <http://www.gnu.org/licenses/>.

import cherrypy
import popculture
from popculture import RevisionStats
import httplib

conf = {'global':{'server.socket_host': '0.0.0.0',
                  'engine.timeout_monitor.on': False}}

class HelloWorld(object):
    @cherrypy.expose
    def reqpage(self, title='Main Page', granularity='section', start=None, end=None, host='localhost', path='/mediawiki/', section=None):
        cherrypy.response.headers['Content-Type'] = 'application/json'
        cherrypy.response.headers['Access-Control-Allow-Origin'] = '*'

        try:
            while True:
                try:
                    if granularity == 'section':
                        res = popculture.revision_stats_json(title, int(start), int(end), host, path)
                    else:
                        res = popculture.article_stats_json(title, int(start), int(end), host, path)

                    res = '[{0}, {1}, {2}]'.format(popculture.article_stats_json(title, int(start), int(end), host, path),
                                                   res,
                                                   popculture.talk_stats_json(title, int(start), int(end), host, path))

                    return res
                except httplib.CannotSendRequest:
                    pass
                except httplib.ResponseNotReady:
                    pass
        except popculture.PageNotFound:
            return '404'

    @cherrypy.expose
    def revcontent(self, revid, host='localhost', path='/mediawiki/', section=None):

        cherrypy.response.headers['Content-Type'] = 'text/html'
        cherrypy.response.headers['Access-Control-Allow-Origin'] = '*'

        if section is None:
            res = popculture.revision_content(int(revid), host, path)
        else:
            res = popculture.revision_content(int(revid), host, path, section=int(section))
            
        return res

    @cherrypy.expose
    def revdiff(self, revid, host='localhost', path='/mediawiki/', section=None):

        cherrypy.response.headers['Content-Type'] = 'text/html'
        cherrypy.response.headers['Access-Control-Allow-Origin'] = '*'

        if section is None:
            res = popculture.revision_diff(int(revid), host, path)
        else:
            res = popculture.revision_diff(int(revid), host, path, section=int(section))
            
        return res

    @cherrypy.expose
    def progress(self, host='localhost', path='/mediawiki/', page='Main Page'):

        cherrypy.response.headers['Content-Type'] = 'text/html'
        cherrypy.response.headers['Access-Control-Allow-Origin'] = '*'
        res = popculture.status(page, host, path)
        return res



cherrypy.quickstart(HelloWorld(), config=conf)
